package com.googlecode.c4e.ui.adapters;

import java.util.List;

import org.apache.maven.continuum.xmlrpc.client.ContinuumXmlRpcClient;
import org.apache.maven.continuum.xmlrpc.project.ProjectSummary;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.model.IWorkbenchAdapter;

import com.googlecode.c4e.ContinuumHelper;
import com.googlecode.c4e.model.Project;
import com.googlecode.c4e.model.ProjectGroup;

public class ProjectGroupAdapter implements IWorkbenchAdapter {

  public Object[] getChildren(Object obj) {
    try {
      ProjectGroup projectGroup = (ProjectGroup) obj;
      ContinuumXmlRpcClient client = ContinuumHelper.createClient(projectGroup.getProfileId());
      List<ProjectSummary> projects = client.getProjects(projectGroup.getId());
      Project[] result = new Project[projects.size()];
      for (int i = 0; i < projects.size(); i++) {
        result[i] = new Project(projects.get(i), projectGroup.getProfileId());
      }
      return result;
    } catch (Exception cce) {
      cce.printStackTrace();
    }
    return new Object[0];
  }

  public ImageDescriptor getImageDescriptor(Object obj) {
    // TODO Auto-generated method stub
    return null;
  }

  public String getLabel(Object obj) {
    try {
      ProjectGroup projectGroup = (ProjectGroup) obj;
      return projectGroup.getName();
    } catch (ClassCastException cce) {
      return null;
    }
  }

  public Object getParent(Object obj) {
    System.err.println("getParent" + getClass().getName());
    // TODO Auto-generated method stub
    return null;
  }

}
